import time

import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait


@pytest.fixture
def driver():
    driver = webdriver.Chrome()  # 创建dirver对象：启动浏览器
    driver.maximize_window()  # 最大化
    yield driver
    driver.quit()

@pytest.mark.main
def test_a(driver):
    driver.get('http://116.62.63.211/shop/user/loginInfo.html')
    el_name = driver.find_element(By.XPATH, '/html/body/div[4]/div/div[2]/div[2]/div/div/div[1]/form/div[1]/input')
    el_name.send_keys("21312313")
    el_password = driver.find_element(By.XPATH,
                                      '/html/body/div[4]/div/div[2]/div[2]/div/div/div[1]/form/div[2]/div/input')
    el_password.send_keys("sdoasbjdfoajsoqpw3234")
    el_btn = driver.find_element(By.XPATH, '/html/body/div[4]/div/div[2]/div[2]/div/div/div[1]/form/div[3]/button')
    el_btn.click()

    # time.sleep(2)
    # 显式等待
    # 1、创建WebDriverWait对象
    wait = WebDriverWait(driver, 10)

    # 2、在函数中判断就绪条件
    def f(d):
        # 如果就绪，返回True
        # 如果没有就绪，返回False
        el = d.find_element(By.XPATH, '/html/body/div[10]/div/p')
        if el.text == "":
            return False
        return True

    # 3、把函数传递给对象，进行等待
    wait.until(f)
    el = driver.find_element(By.XPATH, '/html/body/div[10]/div/p')
    assert el.text == "密码格式 6~18 个字符"
